package com.huidao.service;

import java.util.List;

import com.huidao.dao.RoleDao;
import com.huidao.dao.RoleDaoImpl;
import com.huidao.entity.Role;

public class RoleServiceImpl implements RoleService {
	int pageSize = 5;
	RoleDao dao = new RoleDaoImpl();

	public String addRole(Role role) {
		if(dao.addRole(role)){
			return "";
		}else{
            return "添加失败";
		}
	}

	public String delRole(Long id) {
		if(dao.delRole(id)){
			return "";
		}else{
			return "删除失败";
		}
	}

	public String updateRole(Role role) {
		if(dao.updateRole(role)){
			return "";
		}else{
			return "修改失败";
		}
	}

	public List<Role> getListPage(int currentPage, String namekey) {
		return dao.getListPage(currentPage, pageSize, namekey);
	}

	public int getTotalpage(String namekey) {
		int count = dao.getTotalCount(namekey);
		if(count%pageSize==0){
			return count/pageSize;
		}else{
		    return count/pageSize+1;
		}
	}

	public List<Role> getAllList() {

		return dao.getAllList();
	}

	@Override
	public String addRoleFunction(Long roleid, String[] menuIds) {
		// TODO Auto-generated method stub
		//判断之前是否添加过权限
				boolean isAdded = dao.checkRoleFunction(roleid);
				if(isAdded){//添加过
					if(dao.delRoleFunction(roleid)){//删除之前所选的权限
						if(dao.addRoleFunction(roleid,menuIds)){//添加新的权限列表
							return "";
						}
					}
				}else{
					if(dao.addRoleFunction(roleid,menuIds)){
						return "";
					}
				}
				return "权限设置失败";
	}

	@Override
	public boolean isRoleFunction(Long roleId, String url) {
		// TODO Auto-generated method stub
		return dao.isRoleFunction(roleId, url);
	}

}
